package com.beep.erp.common.base.simpleactivity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


import com.beep.erp.R;
import com.beep.erp.common.base.BaseFragment;
import com.beep.erp.common.base.ToolbarActivity;
import com.beep.erp.common.utils.ToastUtils;

import java.lang.ref.WeakReference;

import butterknife.BindView;


/**
 * FileName: SimpleBackActivity
 * description:
 * Author: Jolly
 * Date: 2018/8/22
 */
public class SimpleBackActivity extends ToolbarActivity {

    public final static String BUNDLE_KEY_PAGE = "BUNDLE_KEY_PAGE";
    public final static String BUNDLE_KEY_BACK = "BUNDLE_KEY_BACK";
    public final static String BUNDLE_KEY_SHOW_EDIT = "BUNDLE_KEY_SHOW_EDIT";
    public final static String BUNDLE_KEY_ARGS = "BUNDLE_KEY_ARGS";
    public final static String BUNDLE_KEY_TITLE = "BUNDLE_KEY_TITLE";
    public final static String BUNDLE_KEY_SEARCH = "BUNDLE_KEY_SEARCH";
    private static final String TAG = "FLAG_TAG";
    protected WeakReference<Fragment> mFragment;

    @BindView(R.id.et_search)
    EditText etSearch;
    protected int mPageValue = -1;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.container)
    FrameLayout container;
    private boolean back = true;

    public static void showSimpleBack(Context context, SimpleBackPage page) {
        Intent intent = new Intent(context, SimpleBackActivity.class);
        intent.putExtra(BUNDLE_KEY_PAGE, page.getValue());
        context.startActivity(intent);
    }

    public static void showSimpleBack(Context context, SimpleBackPage page, boolean isBack) {
        Intent intent = new Intent(context, SimpleBackActivity.class);
        intent.putExtra(BUNDLE_KEY_PAGE, page.getValue());
        intent.putExtra(BUNDLE_KEY_BACK, isBack);
        context.startActivity(intent);
    }

    public static void showSimpleBack(Context context, SimpleBackPage page, boolean isBack, boolean isShowToolBar) {
        Intent intent = new Intent(context, SimpleBackActivity.class);
        intent.putExtra(BUNDLE_KEY_PAGE, page.getValue());
        intent.putExtra(BUNDLE_KEY_BACK, isBack);
        context.startActivity(intent);
    }

    public static void showSimpleBack(Context context, SimpleBackPage page, boolean isBack, boolean isShowEdit, String searchKey) {
        Intent intent = new Intent(context, SimpleBackActivity.class);
        intent.putExtra(BUNDLE_KEY_PAGE, page.getValue());
        intent.putExtra(BUNDLE_KEY_BACK, isBack);
        intent.putExtra(BUNDLE_KEY_SHOW_EDIT, isShowEdit);
        intent.putExtra(BUNDLE_KEY_SEARCH, searchKey);
        context.startActivity(intent);
    }

    public static void showSimpleBack(Context context, SimpleBackPage page,
                                      Bundle args) {
        Intent intent = new Intent(context, SimpleBackActivity.class);
        intent.putExtra(BUNDLE_KEY_ARGS, args);
        intent.putExtra(BUNDLE_KEY_PAGE, page.getValue());
        context.startActivity(intent);
    }

    public static void showSimpleBack(Context context, SimpleBackPage page,
                                      Bundle args, String title) {
        Intent intent = new Intent(context, SimpleBackActivity.class);
        intent.putExtra(BUNDLE_KEY_ARGS, args);
        intent.putExtra(BUNDLE_KEY_PAGE, page.getValue());
        intent.putExtra(BUNDLE_KEY_TITLE, title);
        context.startActivity(intent);
    }

    @Override
    protected boolean isBack() {
        return getIntent().getBooleanExtra(BUNDLE_KEY_BACK, true);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_simple_fragment;
    }

    @Override
    protected void initView() {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {//4.4以下取消硬件 加速
            container.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        if (mPageValue == -1) {
            mPageValue = getIntent().getIntExtra(BUNDLE_KEY_PAGE, 0);
        }
        initFromIntent(mPageValue, getIntent());
    }


    protected void initFromIntent(int pageValue, Intent data) {
        if (data == null) {
            throw new RuntimeException(
                    "you must provide a page info to display");
        }
        SimpleBackPage page = SimpleBackPage.getPageByValue(pageValue);
        if (page == null) {
            throw new IllegalArgumentException("can not find page by value:"
                    + pageValue);
        }

        if (!data.getBooleanExtra(BUNDLE_KEY_BACK, true)) {
            getToolbar().setVisibility(View.GONE);

        }


        String title = data.getStringExtra(BUNDLE_KEY_TITLE);
        if (title != null) {
            getTvTitle().setText(title);
        } else {
            getTvTitle().setText(page.getTitle());
        }

        try {
            Fragment fragment = (Fragment) page.getClz().newInstance();

            Bundle args = data.getBundleExtra(BUNDLE_KEY_ARGS);
            if (args != null) {
                fragment.setArguments(args);
            }

            FragmentTransaction trans = getSupportFragmentManager()
                    .beginTransaction();
            trans.replace(R.id.container, fragment, TAG);
            trans.commitAllowingStateLoss();

            mFragment = new WeakReference<>(fragment);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException(
                    "generate fragment error. by value:" + pageValue);
        }
    }

    @Override
    public void onBackPressed() {
        if (mFragment != null && mFragment.get() != null
                && mFragment.get() instanceof BaseFragment) {
            BaseFragment bf = (BaseFragment) mFragment.get();
            if (!bf.onBackPressed()) {
                super.onBackPressed();
            }
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.ACTION_DOWN
                && mFragment.get() instanceof BaseFragment) {
            ((BaseFragment) mFragment.get()).onBackPressed();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

}
